﻿using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using MichMan.Utilities;
using Subsonic.ViewModels;
using MichMan.Utilities.Requests;
using System.Collections.Generic;
using System.Reflection;

namespace Subsonic.Controls
{
    public class MediaApplicationPage : PhoneApplicationPage
    {
        public MediaApplicationPage()
        {
            // Call initialize component before creating the app bar.  Otherwise, the layout does not update.
            MethodInfo init = GetType().GetMethod("InitializeComponent");
            if (init != null)
            {
                init.Invoke(this, null);
            }

            _mediaPivot = new Finder<MediaPivot>(this);
            _mediaApplicationBar = new MediaApplicationBar(this);
        }

        public const string PivotRequestUri = "PivotRequestUri";
        public const string CurrentId = "CurrentId";
        public const string PivotScroll = "PivotScroll";
        private Dictionary<string, object> pageState = new Dictionary<string, object>();

        internal Dictionary<string, object> PageState { get { return pageState; } }

        protected Changed<Guid> applicationRunId = new Changed<Guid>(() => ApplicationRunId.Instance.Value);
        
        private MediaApplicationBar _mediaApplicationBar;

        protected virtual PivotViewModel CreateViewModel(string firstPivotId) { return null; }

        private Finder<MediaPivot> _mediaPivot = null;

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (_mediaPivot.Value() != null)
            {
                pageState[PivotScroll] = _mediaPivot.Value().ScrollAmount;
            }

            foreach (var key in pageState.Keys)
            {
                this.State[key] = pageState[key];
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            bool changed = applicationRunId.Value;

            pageState.Clear();
            foreach (var key in pageState.Keys)
            {
                this.State[key] = pageState[key];
            } 
            
            if (changed || DataContext == null)
            {
                PivotViewModel pvm = CreateViewModel(State.ContainsKey(CurrentId) ? (string)State[CurrentId] : null);
                pvm.Loaded += new EventHandler(pvm_Loaded);
                DataContext = pvm;
                if (_mediaPivot.Value() != null)
                {
                    _mediaPivot.Value().LoadedPivotItem += new EventHandler<PivotItemEventArgs>(MediaApplicationPage_LoadedPivotItem);
                    IRequest req = _mediaPivot.Value().LoadPivots(pvm);
                    if (req != null)
                    {
                        pageState[PivotRequestUri] = req.Uri;
                    }
                }
                else if (pvm != null)
                {
                    pvm.Load();
                }
            }
        }

        void MediaApplicationPage_LoadedPivotItem(object sender, PivotItemEventArgs e)
        {
            BasePivotItemViewModel pivot = (BasePivotItemViewModel)e.Item.DataContext;
            if (pivot.Id != null)
            {
                pageState[CurrentId] = pivot.Id;
            }
        }

        void pvm_Loaded(object sender, EventArgs e)
        {

            var firstPivot = ((PivotViewModel)DataContext).Pivots.FirstOrDefault();
            if (firstPivot != null && firstPivot.Id != null)
            {
                pageState[CurrentId] = firstPivot.Id;
            }
        }
    }
}
